package com.crois.barrier.transport.heartbeat;

import com.crois.barrier.common.utils.SpiLoader;
import com.crois.barrier.transport.transport.HeartBeatSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HeartbeatSenderProvider {

    private static final Logger LOGGER = LoggerFactory.getLogger(HeartbeatSenderProvider.class);

    private static HeartBeatSender heartbeatSender = null;

    static {
        resolveInstance();
    }

    private static void resolveInstance() {
        HeartBeatSender resolved = SpiLoader.loadHighestPriorityInstance(HeartBeatSender.class);
        if (resolved == null) {
            LOGGER.warn("[HeartbeatSenderProvider] WARN: No existing HeartbeatSender found");
        } else {
            heartbeatSender = resolved;
            LOGGER.info("[HeartbeatSenderProvider] HeartbeatSender activated: " + resolved.getClass()
                    .getCanonicalName());
        }
    }

    /**
     * Get resolved {@link HeartBeatSender} instance.
     *
     * @return resolved {@code HeartbeatSender} instance
     */
    public static HeartBeatSender getHeartbeatSender() {
        return heartbeatSender;
    }

    private HeartbeatSenderProvider() {}
}
